﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZOVNP.Model.Abstracts;

namespace ZOVNP.WinView
{
    public partial class OrderBodyViewControl : UserControl
    {
        private DataGridView _grid;

        public OrderBodyViewControl(IOrder order)
        {
            Order = order;

            InitializeComponent();

            _grid = new DataGridView();
            _grid.ReadOnly = true;
            _grid.AllowUserToAddRows = false;
            _grid.AllowUserToDeleteRows = false;
            _grid.AllowUserToOrderColumns = false;

            var textBoxTemplate = new DataGridViewTextBoxCell();
            _grid.Columns.Add(new DataGridViewColumn(textBoxTemplate)
                                  {Name = "ProductName", HeaderText = "Наименование", Width = 200, ReadOnly = true});
            _grid.Columns.Add(new DataGridViewColumn(textBoxTemplate) {Name = "MaterialName", HeaderText = "Материал", Width = 200, ReadOnly = true});
            _grid.Columns.Add(new DataGridViewColumn(textBoxTemplate) {Name = "Amount", HeaderText = "Количество", Width = 75, ReadOnly = true});
            _grid.Columns.Add(new DataGridViewColumn(textBoxTemplate) {Name = "Description", HeaderText = "Примечание", Width = 100, ReadOnly = true});
            _grid.Columns.Add(new DataGridViewColumn(textBoxTemplate) {Name = "Cost", HeaderText = "Цена", Width = 100, ReadOnly = true});
            _grid.Dock = DockStyle.Fill;

            ResetGrid();

            Controls.Add(_grid);
        }

        public readonly IOrder Order;

        private DataGridViewRow CreateRow()
        {
            DataGridViewRow result = new DataGridViewRow();
            result.CreateCells(_grid);
            return result;
        }

        private void ResetGrid()
        {
            _grid.Rows.Clear();

            bool needEmptyRowToAdd = false;
            var orderParts = Order.OrderParts.Where(op => op.OrderItems.Any()).OrderBy(op => op.OrderPartHelp.OrderNumber);
            foreach (var orderPart in orderParts)
            {
                if (needEmptyRowToAdd)
                    _grid.Rows.Add(CreateRow());
                needEmptyRowToAdd = true;

                DataGridViewRow orderPartRow = CreateRow();
                orderPartRow.Cells[0].Value = orderPart.OrderPartHelp.Name;
                orderPartRow.Cells[0].Style.Font = new Font(Font, FontStyle.Bold);
                orderPartRow.Tag = orderPart;
                _grid.Rows.Add(orderPartRow);

                foreach (var orderItem in orderPart.OrderItems)
                {
                    DataGridViewRow orderItemRow = CreateRow();

                    orderItemRow.Cells[0].Value = orderItem.PriceItem.Product.Name;
                    orderItemRow.Cells[1].Value = orderItem.PriceItem.Material.Name;
                    orderItemRow.Cells[2].Value = orderItem.Amount;
                    orderItemRow.Cells[3].Value = orderItem.Description;
                    orderItemRow.Cells[4].Value = orderItem.GetCost().ToString("F");
                    _grid.Rows.Add(orderItemRow);
                }
            }
        }
    }
}
